package com.nanxhs.juc;

import org.junit.Test;

import java.util.HashMap;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.LockSupport;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author 唐海斌
 * @version 1.0
 * @date 2021/5/11 9:48
 */
public class AQSDebug {

    int a = 0;

    Lock lock = new ReentrantLock();

    public static void main(String[] args) throws InterruptedException {
        new AQSDebug().test();
    }

    public void test() throws InterruptedException {
        Thread threadA = new Thread(() -> {
            lock.lock();
            System.out.println(Thread.currentThread().getName() + "-获取到锁");
            a = 1;
            LockSupport.parkNanos(TimeUnit.SECONDS.toNanos(10));
//            LockSupport.park();
            lock.unlock();
        });
        threadA.setName("线程A");
        threadA.start();
        LockSupport.parkNanos(TimeUnit.SECONDS.toNanos(2));

        Thread threadB = new Thread(() -> {
            lock.lock();
            System.out.println(Thread.currentThread().getName() + "-获取到锁");
            a = 2;
            lock.unlock();
        });
        threadB.setName("线程B");
        threadB.start();
        LockSupport.parkNanos(TimeUnit.SECONDS.toNanos(2));

        Thread threadC = new Thread(() -> {
            lock.lock();
            System.out.println(Thread.currentThread().getName() + "-获取到锁");
            a = 3;
            lock.unlock();
        });
        threadC.setName("线程C");
        threadC.start();
        LockSupport.parkNanos(TimeUnit.SECONDS.toNanos(2));

        Thread threadD = new Thread(() -> {
            lock.lock();
            System.out.println(Thread.currentThread().getName() + "-获取到锁");
            a = 4;
            lock.unlock();
        });
        threadD.setName("线程D");
        threadD.start();
    }

    @Test
    public void test2() {
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("name", "xiaoming");
        hashMap.put("age", "27");
    }
}
